<template>
	<view>
		<!-- 头部 -->
		<view class="navbar">
			<u-icon @click="navigateBack" class="icon-left" name="arrow-left" color="#fff" size="50"></u-icon>
			<text>设备列表</text>
		</view>
		<!-- 内容 -->
		<view class="section">
			<view class="information" v-for='(item,idx) in deviceList' :key='idx'>
				<u-icon v-show="choosedIdx.includes(idx)" @click="changeImg(idx)"
					style="position: absolute;left: 1%;top: 5%;" class="icon-left" name="checkmark-circle-fill"
					color="#007AFF" size="50"></u-icon>
				<view v-show="!choosedIdx.includes(idx)" @click="changeImg(idx)"
					style="width: 45rpx;height: 45rpx;border-radius: 50%;border: #007AFF 1px solid;position: absolute;left: 2%;top: 6%;">
				</view>
				<image :src="item.headurl" mode=""></image>
				<view class="text">
					<text>{{item.name}}</text>
					<u-icon style="padding-left: 10rpx;" name="wifi" color="rgb(68,92,223)" v-show="showUp"
						@click="change" size="40"></u-icon>
					<u-icon style="padding-left: 10rpx;" name="wifi-off" color="rgb(68,92,223)" v-show="!showUp"
						@click="change" size="40"></u-icon>
				</view>
				<u-icon @click="editDequipment" style="position: absolute;right: 5%;top: 15%;" name="edit-pen"
					color="#FFD115" size="40">
				</u-icon>
				<text
					style="position: absolute;bottom: 20%;left:30%;font-size: 30rpx;color: #666666;">当前版本为：{{item.version}}</text>
				<view class="btn">
					<text @click="frame(item)">升级设备</text>
				</view>
			</view>
			<!-- 	<view class="information">
				<u-icon v-show="showUpImg1" @click="changeImg1" style="position: absolute;left: 1%;top: 5%;"
					class="icon-left" name="checkmark-circle-fill" color="#007AFF" size="50"></u-icon>
				<view v-show="!showUpImg1" @click="changeImg1"
					style="width: 45rpx;height: 45rpx;border-radius: 50%;border: #007AFF 1px solid;position: absolute;left: 2%;top: 6%;">
				</view>
				<image src="../../../../static/images/baby.png" mode=""></image>
				<view class="text">
					<text>大宝</text>
					<u-icon style="padding-left: 10rpx;" name="wifi" color="rgb(68,92,223)" v-show="showUp"
						@click="change" size="40"></u-icon>
					<u-icon style="padding-left: 10rpx;" name="wifi-off" color="rgb(68,92,223)" v-show="!showUp"
						@click="change" size="40"></u-icon>
				</view>
				<u-icon @click="editDequipment" style="position: absolute;right: 5%;top: 15%;" name="edit-pen" color="#FFD115" size="40">
				</u-icon>
				<text
					style="position: absolute;bottom: 20%;left:30%;font-size: 30rpx;color: #666666;">当前版本为：{{edition}}</text>
				<view class="btn">
					<text @click="frame">升级设备</text>
				</view>
			</view> -->
			<!-- 			<view class="information">
				<u-icon v-show="showUpImg2" @click="changeImg2" style="position: absolute;left: 1%;top: 5%;"
					class="icon-left" name="checkmark-circle-fill" color="#007AFF" size="50"></u-icon>
				<view v-show="!showUpImg2" @click="changeImg2"
					style="width: 45rpx;height: 45rpx;border-radius: 50%;border: #007AFF 1px solid;position: absolute;left: 2%;top: 6%;">
				</view>
				<image src="../../../../static/images/baby.png" mode=""></image>
				<view class="text">
					<text>大宝</text>
					<u-icon style="padding-left: 10rpx;" name="wifi" color="rgb(68,92,223)" v-show="showUp"
						@click="change" size="40"></u-icon>
					<u-icon style="padding-left: 10rpx;" name="wifi-off" color="rgb(68,92,223)" v-show="!showUp"
						@click="change" size="40"></u-icon>
				</view>
				<u-icon @click="editDequipment" style="position: absolute;right: 5%;top: 15%;" name="edit-pen" color="#FFD115" size="40">
				</u-icon>
				<text
					style="position: absolute;bottom: 20%;left:30%;font-size: 30rpx;color: #666666;">当前版本为：{{edition}}</text>
				<view class="btn">
					<text @click="frame">升级设备</text>
				</view>
			</view> -->

			<!-- 按钮 -->
			<view class="binding">
				<view style="background-color: rgb(68,92,223);color: #fff;" @click="unbound"><text>确定解绑</text>
					<u-toast ref="uToast" />
				</view>
				<view style="border: rgb(68,92,223) 1px solid;color: rgb(68,92,223);" @click="cancel"><text>取消解绑</text>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				choosedIdx: [],
				deviceList: [],
				showUp: true,
				showUpImg: false,
				showUpImg1: false,
				showUpImg2: false,
				edition: 'V1.1.1',
			}
		},
		mounted() {
			this.getDevice()
		},
		methods: {
			async getDevice() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let res = await this.$api.getDeviceList();

				if (res.code == 200) {
					console.log(res, 88)
					this.deviceList = res.result.list
				} else {

				}
				uni.hideLoading();


			},
			navigateBack() {
				uni.reLaunch({
					url: '../equipmentList'
				})
			},
			// 跳至修改信息
			editDequipment() {
				uni.reLaunch({
					url: '../editEquipment/editEquipment'
				})
			},
			change: function() {
				undefined
				this.showUp = !this.showUp
			},
			changeImg: function(idx) {
				if (!this.choosedIdx.includes(idx)) {
					this.choosedIdx.push(idx)
				} else {
					this.choosedIdx.splice(idx, 1)
				}



			},
			changeImg1: function() {
				undefined
				this.showUpImg1 = !this.showUpImg1
			},
			changeImg2: function() {
				undefined
				this.showUpImg2 = !this.showUpImg2
			},
			frame(item) {
				let that = this
				uni.showModal({
					content: '确定升级你的设备吗？',
					success: function(res) {
						if (res.confirm) {
							that.$api.upgradeDevice({
								id: item.id
							}).then(result => {
								if (result.code == 200) {
									uni.showToast({
										title: '升级设备成功！'
									})
								}
							})


						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					},
				});
			},
			unbound() {
				if (this.choosedIdx.length) {
					let that = this
					uni.showModal({
						content: '确定与该设备解除绑定吗？',
						success: function(res) {
							if (res.confirm) {
								that.deviceList.forEach((ele, idx) => {
									if (that.choosedIdx.includes(idx)) {
										that.$api.deviceUnbond({
											id: ele.device_id
										}).then(res=>{
											if(res.code===200){
												uni.showToast({
													title:'解绑成功！',
													duration:1000,
													success: ()=>{
														setTimeout(()=>{
															that.getDevice()
														},1000)
													}
													
												})
												uni.reLaunch({
													url:'../../../index/index'
												})
											}
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						},
					})
				}
			},
			cancel() {
				uni.reLaunch({
					url: '../equipmentList'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	// 头部
	.navbar {
		padding-top: 90rpx;
		padding-bottom: 40rpx;
		width: 750rpx;
		color: #fff;
		background-color: rgb(68, 92, 223);
	}

	.navbar>text {
		font-size: 36rpx;
		padding-left: 220rpx;
	}

	// 内容
	.section {
		background-color: rgb(245, 245, 245);
		padding: 30rpx;
	}

	.information {
		position: relative;
		border-radius: 20rpx;
		padding: 25rpx;
		height: 200rpx;
		background-color: #fff;
		margin-bottom: 30rpx;
		box-shadow: 1px 1px 2px rgb(228, 227, 227), 0 0 5px rgb(228, 227, 227), 0 0 1px rgb(228, 227, 227);
	}

	.information>.text {
		position: absolute;
		top: 20%;
		left: 30%;
		font-size: 32rpx;
		font-weight: bold;
	}

	.information>image {
		width: 120rpx;
		height: 120rpx;
		padding-left: 20rpx;
		position: absolute;
		left: 8%;
		top: 20%;
	}

	.btn {
		width: 150rpx;
		color: #fff;
		text-align: center;
		background-color: rgb(68, 92, 223);
		border-radius: 50rpx;
		padding: 10rpx;
		position: absolute;
		right: 5%;
		top: 55%;
	}

	// 按钮
	.binding {
		margin-top: 100rpx;
		padding: 30rpx;
		padding-bottom: 340rpx;
	}

	.binding>view {
		box-shadow: 1px 1px 2px rgb(228, 227, 227), 0 0 5px rgb(228, 227, 227), 0 0 1px rgb(228, 227, 227);
		border-radius: 50rpx;
		text-align: center;
		margin-bottom: 35rpx;
		padding: 25rpx;
		font-size: 30rpx;
	}
</style>
